package database

import (
	"my-godis/config"
	"my-godis/interface/redis"
	"my-godis/redis/reply"
)
// Ping the server
func Ping(db *DB, args [][]byte) redis.Reply {
	if len(args) == 0 {
		return &reply.PongReply{}
	}else if len(args) == 1 {
		return reply.MakeStatusReply(string(args[0]))
	}else {
		return reply.MakeErrReply("ERR wrong number of arguments for 'ping' command")
	}
}

func Auth(c redis.Connection, args [][]byte) redis.Reply {
	if len(args) != 1 {
		return reply.MakeErrReply("ERR wrong number of arguments for 'auth' command")
	}
	realpwd := config.Properties.RequirePass
	if realpwd == "" {
		return reply.MakeErrReply("ERR Client sent AUTH, but no password is set")
	}
	pwd := string(args[0])
	c.SetPassword(pwd)
	if realpwd != pwd {
		return reply.MakeErrReply("ERR invalid password")
	}
	return reply.MakeOkReply()
}

func isAuthenticated(c redis.Connection) bool {
	realpwd := config.Properties.RequirePass
	if realpwd == "" {
		return true
	}
	return c.GetPassword() == realpwd
}
func init() {
	RegisterCommand("ping", Ping, noPrepare,nil,-1)
}











